﻿using ExamRoomAllocationManagement.Services;
using Microsoft.AspNetCore.Mvc;
using Xmu.ExamRoomAllocationManagement.Services;
using Xmu.Stores.Models;

namespace UserStores.Controllers
{
    [Route("/api/[controller]")]
    public class ExamController : Controller
    {
        private ExamService examService;
        public ExamController(ExamService examService)
        {
            this.examService = examService;
        }

        [HttpGet]
        public IActionResult GetExams([FromQuery] int pageIndex, [FromQuery] int pageSize)
        {
            (object exams, object msg) = examService.GetExams(pageIndex, pageSize);
            return Ok(new
            {
                msg,
                data = exams
            });
        }

        [HttpGet("all")]
        public IActionResult GetAllExams()
        {
            //return Ok(examService.GetAllExams());
            //return Json(examService.GetAllExams());
            (object exams, object msg) = examService.GetAllExams();
            return Ok(new
            {
                msg,
                data = exams
            });
        }

        [HttpGet("{id}")]
        public IActionResult GetExam(Guid id)
        {
            (object exams, object msg) = examService.GetExam(id);
            return Ok(new
            {
                msg,
                data = exams
            });
        }

        [HttpPost]
        public IActionResult AddExam([FromBody] Exam exam)
        {
            Console.WriteLine("exam in controller:" + exam);
            var (success, msg) = examService.AddExam(exam);
            if (success) return Ok();
            else return BadRequest(msg);
        }
    }
}
